package com.fire.core.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fire.core.dao.TMitServiceDicBaseMappers;
import com.fire.core.mode.TMitServiceDicBases;
import com.fire.core.service.ITMitServiceDicBaseServices;
import com.fire.core.service.RedisService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 基础字典表 服务实现类
 * </p>
 *
 * @author MIT123
 * @since 2018-04-26
 */
@Service
public class TMitServiceDicBaseServiceImpls extends ServiceImpl<TMitServiceDicBaseMappers, TMitServiceDicBases>
		implements ITMitServiceDicBaseServices {
	private static final Logger LOGGER = LoggerFactory.getLogger(TMitServiceDicBaseServiceImpls.class);
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private RedisService redisService;
	@Autowired
	private TMitServiceDicBaseMappers tMitServiceDicBaseMappers;

	/**
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean ckeckToken(String token) throws Exception {
		boolean flag = false;
		// 验证token是否超时
		Long oldDate = redisTemplate.getExpire(token, TimeUnit.MINUTES);
		if (oldDate > 0) {
			String oldoken = (String) redisTemplate.opsForValue().get(token);
			if (token.equals(oldoken)) {
				flag = true;
			}
		} else {
			redisTemplate.delete(token);
		}

		return flag;
	}

	/**
	 * 通过bkey获取配置数据(先查询缓存，缓存没数据再向数据库去查)
	 * 
	 * @param bkey
	 * @return TMitServiceDicBases
	 * @throws Exception
	 */
	@Override
	public String getBaseByBkey(String bkey) {
		String baseValue = null;
		Map<String, String> basesMap = (Map<String, String>) redisService.get("basesMap");
		LOGGER.info("获取到的缓存配置数据map" + basesMap);
		if (basesMap != null) {
			if (StringUtils.isNotBlank(basesMap.get(bkey))) {
				LOGGER.info("传入到的code" + bkey);
				baseValue = basesMap.get(bkey);
				LOGGER.info("通过code在缓存里获取到的数据" + baseValue);
			} else {
				baseValue=getValue(bkey);
			}
		} else {
			baseValue=getValue(bkey);

		}

		return baseValue;

	}

	/**
	 * 通过数据库获取配置数据(先查询缓存，缓存没数据再向数据库去查)
	 * 
	 * @param bkey
	 * @return String value
	 * @throws Exception
	 */
	public String getValue(String bkey) {
		String baseValue = null;
		TMitServiceDicBases tMitServiceDicBases = tMitServiceDicBaseMappers.selectById(bkey);
		if (tMitServiceDicBases != null) {
			if (StringUtils.isNotBlank(tMitServiceDicBases.getBvalue())) {
				baseValue = tMitServiceDicBases.getBvalue().trim();
			} else {
				LOGGER.info("该配置信息不完善");
				return null;
			}

		} else {
			LOGGER.info("查询暂无该配置信息");
			return null;
		}
		return baseValue;
	}

	/**
	 * 获取配置数据
	 * 
	 * @param
	 * @return List<TMitServiceDicBases>
	 * @throws Exception
	 */

	@Override
	public Map<String, String> getBaseList() {
		Map<String, String> basesMap = (Map<String, String>) redisService.get("basesMap");
		LOGGER.info("获取到的缓存配置数据map" + basesMap);
		if (basesMap == null) {
			List<TMitServiceDicBases> basesList = tMitServiceDicBaseMappers
					.selectList(new EntityWrapper<TMitServiceDicBases>().isNotNull("bkey"));
			LOGGER.info("通过数据库获取到的配置数据" + basesList);
			if (basesList != null && !basesList.isEmpty()) {
				Map<String, String> map = new HashMap<String, String>();
				for (TMitServiceDicBases base : basesList) {
					map.put(base.getBkey(), base.getBvalue());
				}
				basesMap = map;

			}

		}
		return basesMap;
	}

}
